package com.lanyou.cook.web;

import java.io.IOException;

import javax.servlet.http.Part;

import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.xml.sax.SAXException;

import com.google.gson.JsonObject;
import com.lanyou.cook.common.Constants;
import com.lanyou.cook.service.ImportDataService;
import com.lanyou.esb.exception.ServiceHandlerException;

/**
 * 导入相关业务的控制层
 * @author 杨晨睿
 *
 */
@Controller
@RequestMapping(Constants.BASE_URL_API + "/importData")
public class FileUploadForImportDataController {

	@Autowired
	private ImportDataService importDataService;

	/**
	 * 领域管理的导入
	 * @param name 类型:String 领域管理导入EXCEL名称
	 * @param file	类型:Part 导入的文件
	 * @return 类型:String 导入成功返回ture,导入失败返回false
	 */
	@ResponseBody
	@RequestMapping(value = "/field/fileUpload", method = RequestMethod.POST, produces="text/plain")
	public String uploadForImportField(@RequestParam("name") String name, @RequestParam("importLinkFile") Part file)
			throws IOException, InvalidFormatException, SAXException {
		String str = "";
		try {
			if(name.indexOf(".xls") != -1 || name.indexOf(".xlsx") != -1){
				str = importDataService.importField(file.getInputStream());
			}else{
				JsonObject result = new JsonObject();
				result.addProperty("backValue", "请选择EXCEL文件导入！");
				return result.toString();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return str;
	}
	
	/**
	 * 领域系统管理的导入
	 * @param name 类型:String 领域管理导入EXCEL名称
	 * @param file	类型:Part 导入的文件
	 * @return 类型:String 导入成功返回ture,导入失败返回false
	 */
	@ResponseBody
	@RequestMapping(value = "/field/fieldSystemUpload", method = RequestMethod.POST, produces="text/plain")
	public String uploadForImportFieldSystem(@RequestParam("name") String name, @RequestParam("importLinkFile") Part file)
			throws IOException, InvalidFormatException, SAXException {
		String str = "";
		try {
			if(name.indexOf(".xls") != -1 || name.indexOf(".xlsx") != -1){
				str = importDataService.importFieldSystem(file.getInputStream());
			}else{
				JsonObject result = new JsonObject();
				result.addProperty("backValue", "请选择EXCEL文件导入！");
				return result.toString();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return str;
	}
	
	/**
	 * dblink接口的导入
	 * @param name 类型:String dblink接口导入EXCEL名称
	 * @param file	类型:Part 导入的文件
	 */
	@ResponseBody
	@RequestMapping(value = "/dblinkInterface/fileUpload", method = RequestMethod.POST, produces="text/plain")
	public String uploadForImportDblinkInterface(@RequestParam("name") String name, @RequestParam("importLinkFile") Part file)
			throws IOException, InvalidFormatException, SAXException {
		/*JsonObject result = new JsonObject();*/
		String str = "";
		try {
			str = importDataService.importDBlinkInterface(file.getInputStream());
		} catch (Exception e) {
			e.printStackTrace();
		}
		return str;
	}
	

	/**
	 * 用户管理的导入
	 * @param name 类型:String 用户管理导入EXCEL名称
	 * @param file	类型:Part 导入的文件
	 * @return 类型:String 导入成功返回ture,导入失败返回false
	 */
	@ResponseBody
	@RequestMapping(value = "/user/userUpload", method = RequestMethod.POST, produces="text/plain")
	public String uploadForImportUser(@RequestParam("name") String name, @RequestParam("file") Part file)
			throws IOException, InvalidFormatException, SAXException {
		String str = "";
		try{
			if(name.indexOf(".xls") != -1 || name.indexOf(".xlsx") != -1){
				str = importDataService.importUser(file.getInputStream());
			}else{
				JsonObject result = new JsonObject();
				result.addProperty("backValue", "请选择EXCEL文件导入！");
				return result.toString();
			}
		}catch(Exception e){
			e.printStackTrace();
		}
		
		return str;
	}
	
	/**
	 * dblink管理的导入
	 * @param name 类型:String dblink管理导入EXCEL名称
	 * @param file	类型:Part 导入的文件
	 * @return 类型:String 导入成功返回ture,导入失败返回false
	 */
	@ResponseBody
	@RequestMapping(value = "/dblink/fileUpload", method = RequestMethod.POST, produces="text/plain")
	public String uploadForImportDBlink(@RequestParam("name") String name, @RequestParam("importLinkFile") Part file)
			throws IOException, InvalidFormatException, SAXException {
		/*JsonObject result = new JsonObject();*/
		String str = "";
		try {
			str = importDataService.importDBlink(file.getInputStream());
		} catch (Exception e) {
			e.printStackTrace();
		}
		return str;
	}
	
	
	@ResponseBody
	@RequestMapping(value = "/httpInterface/fileUpload", method = RequestMethod.POST, produces="text/plain")
	public String uploadForImportHttp(@RequestParam("name") String name, @RequestParam("file") Part file)
			throws IOException, InvalidFormatException, SAXException {
		JsonObject result = new JsonObject();
		String str = "";
		try {
			str = importDataService.importHttp(file.getInputStream());
		} catch (ServiceHandlerException e) {
			result.addProperty("backValue", false);
			result.addProperty("errorDesc", e.getMessage());
			return result.toString();
		}
		return str;
	}
	
	
	@ResponseBody
	@RequestMapping(value = "/ftpInterface/fileUpload", method = RequestMethod.POST, produces="text/plain")
	public String uploadForImportFtp(@RequestParam("name") String name, @RequestParam("file_ftp") Part file)
			throws IOException, InvalidFormatException, SAXException {
		JsonObject result = new JsonObject();
		String str = "";
		try {
			str = importDataService.importFtp(file.getInputStream());
		} catch (ServiceHandlerException e) {
			result.addProperty("backValue", false);
			result.addProperty("errorDesc", e.getMessage());
			return result.toString();
		}
		return str;
	}
}
